Skip to content

Conversation

@paldepind
Copy link
Contributor

@paldepind paldepind commented Nov 11, 2025

This re-applies the reverted #20645 and fixes a bug that caused nrOfBoundsNEPhi to be non-functional. For a given v and def the property isNEPhi(v, def, access, _) might hold for many accesses. Hence we can't use exists for access. Using max instead ensures that we only propagate bounds for a single access.

nrOfBoundsUnsupportedGuardPhi suffered from the same problem. I haven't seen any issues arise in practice from that, but I gave it the same treatment for good measure.

A QA run has showed that this fixes the previously seen timeouts. We now instead gain 7 stable progressions on BMN and 14 stable progressions on traced extraction, at the cost of slowdowns in the 0.8 - 1.8x range on 3 projects.

@github-actions github-actions bot added the C++ label Nov 11, 2025
}

float debugNrOfBounds(Expr e) {
e = getRelevantLocatable() and

Check warning

Code scanning / CodeQL

Missing QLDoc for parameter Warning

The QLDoc has no documentation for e, but the QLDoc mentions nrOfBounds
* should be empty, so this predicate is useful to debug non-functional cases.
*/
int nonFunctionalNrOfBounds(Expr e) {
strictcount(BoundsEstimate::nrOfBoundsExpr(e)) > 1 and

Check warning

Code scanning / CodeQL

Missing QLDoc for parameter Warning

The QLDoc has no documentation for n, but the QLDoc mentions nrOfBounds
* Finds any expressions for which `nrOfBounds` is not functional. The result
* should be empty, so this predicate is useful to debug non-functional cases.
*/
private predicate nonFunctionalNrOfBounds(Expr e) {

Check warning

Code scanning / CodeQL

Missing QLDoc for parameter Warning test

The QLDoc has no documentation for e, but the QLDoc mentions nrOfBounds
@paldepind paldepind force-pushed the cpp/range-analysis-perf-fix branch from 88b9fc4 to ffe9839 Compare November 12, 2025 08:11
@paldepind paldepind force-pushed the cpp/range-analysis-perf-fix branch from ffe9839 to 1dd78e2 Compare November 14, 2025 11:33
@paldepind paldepind marked this pull request as ready for review November 14, 2025 11:44
@paldepind paldepind requested a review from a team as a code owner November 14, 2025 11:44
Copilot AI review requested due to automatic review settings November 14, 2025 11:44
Copilot finished reviewing on behalf of paldepind November 14, 2025 11:47
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR improves the performance of C++ range analysis by fixing a bug that caused nrOfBoundsNEPhi and nrOfBoundsUnsupportedGuardPhi to be non-functional. The fix uses max instead of exists to ensure only a single access is considered when multiple accesses exist for a given variable and definition. This prevents combinatorial explosion during range analysis and fixes previously observed timeouts.

Key changes:

  • Introduces bound estimation module to predict when widening is needed
  • Adds helper functions for consistent widening application
  • Fixes non-functional predicates using max aggregate
  • Adds comprehensive test cases for performance edge cases

Reviewed Changes

Copilot reviewed 8 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
cpp/ql/lib/semmle/code/cpp/rangeanalysis/SimpleRangeAnalysis.qll Core implementation with bounds estimation module, widening helpers, and performance optimizations
cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/test.c New test functions for repeated if statements, NE phi nodes, and nested guards
cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/nrOfBounds.ql New test query to verify functionality of bounds estimation
cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/*.expected Updated expected test outputs reflecting the fix
cpp/ql/lib/change-notes/2025-11-11-range-analysis-performance.md Change note documenting the performance fix

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Contributor

@jketema jketema left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Just for completeness would you mind running DCA on this?

@paldepind
Copy link
Contributor Author

Yes, of course.

@jketema jketema merged commit 0f2e29c into github:main Nov 14, 2025
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants